<template>
    <div class="add-activity w-100 h-100">
        <el-card class="box-card-table h-100" shadow="never">
            <div slot="header" class="clearfix">
                <span class="fl">添加活动</span>
                <el-button class="fr" @click="goback" style="padding:6px;">返回</el-button>
            </div>
            <div class="h-100" v-loading="loading">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="活动名称：" prop="bA_Name">
                        <el-input v-model.trim="ruleForm.bA_Name" placeholder="活动名称"></el-input>
                    </el-form-item>
                    <el-form-item label="发布人：" prop="bA_Username">
                        <el-input style="width:220px;" v-model.trim="ruleForm.bA_Username" placeholder="请输入发布人"></el-input>
                        <!-- <el-select v-model="ruleForm.bA_Username" placeholder="请选择教师">
                            <el-option :label="item.T_Name" :value="item.TID" v-for="item in teacherList" :key="item.TID"></el-option>
                        </el-select> -->
                    </el-form-item>
                    <el-form-item label="活动时间：" prop="bA_Time">
                        <el-date-picker
                        v-model="ruleForm.bA_Time"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="封面图片：" prop="bA_Photo">
                        <el-upload
                        class="avatar-uploader"
                        :action="baseurl"
                        :http-request="uploadFilesHandler"
                        :show-file-list="false"
                        :before-upload="beforeAvatarUpload">
                            <img v-if="ruleForm.bA_Photo" :src="ruleForm.bA_Photo" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="活动内容：" prop="bA_Content">
                        <!-- <quill-editor v-model="ruleForm.bA_Content"
                            style="line-height:0px;"
                            ref="myQuillEditor"
                            :options="editorOption">
                        </quill-editor> -->
                        <ele-editor editorId=“activityEditor” v-model="ruleForm.bA_Content"></ele-editor>
                    </el-form-item>
                    <el-form-item class="t-r" style="margin-top:10px;">
                        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
import elewarp from '@/components/warp-slot.vue'
// 添加活动
import {addactivityList,getactivityInfo,editactivityinfo,baseurl,upLoadFiles} from '@/api/index.js'
import eleEditor from '@/components/editor.vue'
export default {
    name: 'addsummary',
    components: {
        elewarp,
        eleEditor
    },
    data() {
        return {
            loading:false,
            baseurl:baseurl + '/UploadPhoto',
            ruleForm:{
                bA_Name:'',
                bA_Photo:'',
                bA_Content:'',
                bA_Time:'',
                bA_Username:''
            },
            // editorOption:{
            //     modules:{
            //         toolbar:[
            //             ["bold", "italic", "underline", "strike"],
            //             ["code-block",{list: "ordered"},{list: "bullet"}],
            //             [{"color":[]},{"background":[]}],
            //             [{indent: "-1"},{indent: "+1"}],
            //             [{align: []}],
            //             ["link", "image"]
            //         ]
            //     },
            //     placeholder:'请输入'
            // },
            rules:{
                bA_Name:[
                    { required: true, message: '名称不能为空', trigger: 'blur' },
                    {min:3,max:30,message:'长度再3-30个字符之间',trigger: 'blur'}
                ],
                bA_Username:[
                    { required: true, message: '发布人不能为空', trigger: 'blur' },
                    {min:2,max:10,message:'长度再2-10个字符之间',trigger: 'blur'}
                ],
                bA_Content:[
                    { required: true, message: '内容不能为空', trigger: 'blur' }
                ],
                bA_Time:[
                    { required: true, message: '活动时间不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    computed:{
        teacherList() {
            return this.$store.state.allTeacherList;
        }
    },
    created(){
        this.activityid = this.$route.query.id;
        if(this.activityid) {
            this._getactivityInfo();
        }
    },
    methods:{
        submitForm(){
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params ={
                        ...this.ruleForm
                    }
                    if(this.activityid) {
                        editactivityinfo({...params,bAID:this.activityid}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','修改成功！');
                                    this.goback()
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    } else {
                        addactivityList({...this.ruleForm}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','添加成功！');
                                    this.goback()
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch(err => {
                            
                        })
                    }
                } else {
                    return false;
                }
            })
            
        },
        uploadFilesHandler(options){;
            let file = options.file
            let filename = options.file.name;
            let type = options.file.type;
            if (file) {
                var ready = new FileReader();
                ready.readAsDataURL(file);
                ready.onload = (fileReader) => {
                    let base64Str = fileReader.currentTarget.result;
                    this.loading = true
                    upLoadFiles({filebytes:base64Str,bExt:type}).then((res) => {;
                        if(res.code == 200) {
                            this.ruleForm.bA_Photo = res.FileName;
                        } else {
                            this.$message.error(res.message);
                        }
                    }).finally (() => {
                        this.loading = false;
                    })
                
                }
            }
        },
        
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 、PNG格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        _getactivityInfo(){
            getactivityInfo({bAID:this.activityid}).then((res) => {
                if(res) {
                    for (const key in this.ruleForm) {
                        if (this.ruleForm.hasOwnProperty(key)) {
                            let _key = key.substr(1);
                            if( _key in res) {
                                this.ruleForm[key] = res[_key];
                            }
                        }
                    }
                }
            })
        },
        onReset(){
            this.$refs.searchform.resetFields()
        },
        // 全选
        goback(){
            this.$router.push({path:'/activity/activity-list'})
        },
    }
}
</script>
<style lang="less">

.add-activity{
    .el-card__body{
        height:calc(100% - 70px);
        box-sizing: border-box;
    }
    box-sizing: border-box;
    .line-height{
        line-height: 20px;
    }
    .ql-container.ql-snow{
        min-height: 300px;
    }
    .el-card{
        box-sizing: border-box;
    }
    .box-card-table{
        overflow-y: auto;
        box-sizing: border-box;
    }
}
</style>
